package com.steven.sync.mianshi;

public class RoundTable_ChiFan {


    static class KuaiZi {
        private Integer index;


        public KuaiZi() {
        }

        public KuaiZi(int i) {
            this.index = i;
        }
    }


  public   static class QiShi extends Thread {
        private Integer index;
        private KuaiZi left;
        private KuaiZi right;

        public QiShi(Integer index, KuaiZi left, KuaiZi right) {
            this.index = index;
            this.left = left;
            this.right = right;
        }

        public QiShi() {

        }

        public void run() {
            if( (index & 1)==1 ){
                synchronized (left) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (right) {
                        try {
                            Thread.sleep(700);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(index + ":骑士吃完了");
                    }
                }
            }else {
                synchronized (right) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (left) {
                        try {
                            Thread.sleep(300);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(index + ":骑士吃完了");
                    }
                }
            }

        }
    }

    public static void main(String[] args) {

        final KuaiZi kuaiZi0 = new KuaiZi(0);
        final KuaiZi kuaiZi1 = new KuaiZi(1);
        final KuaiZi kuaiZi2 = new KuaiZi(2);
        final KuaiZi kuaiZi3 = new KuaiZi(3);
        final KuaiZi kuaiZi4 = new KuaiZi(4);

        QiShi qiShi0 = new QiShi(0, kuaiZi4, kuaiZi0);
        QiShi qiShi1 = new QiShi(1, kuaiZi0, kuaiZi1);
        QiShi qiShi2 = new QiShi(2, kuaiZi1, kuaiZi2);
        QiShi qiShi3 = new QiShi(3, kuaiZi2, kuaiZi3);
        QiShi qiShi4 = new QiShi(4, kuaiZi3, kuaiZi4);

        qiShi0.start();
        qiShi1.start();
        qiShi2.start();
        qiShi3.start();
        qiShi4.start();


    }


}
